package com.mayuanfei.springboot12.config;

import com.hazelcast.config.*;
import com.mayuanfei.springboot12.common.Constants;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * hazelcast配置类
 *
 * @author 老马
 * @date 2023-07-07 14:17
 */
@EnableCaching
@Configuration
public class HazelcastConfig {

    @Bean
    public Config config() {
        Config config = new Config();
        config.setInstanceName("hazelcast-instance");
        config.setClusterName("dev");

        // 设置驱逐策略
        EvictionConfig evictionConfig = new EvictionConfig();
        evictionConfig.setEvictionPolicy(EvictionPolicy.LFU);
        evictionConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
        evictionConfig.setSize(542);

        // 设置map配置
        MapConfig mapUserConfig = new MapConfig();
        mapUserConfig.setName(Constants.CACHE_NAME_SESSION_USERS)
                .setBackupCount(2)
                .setTimeToLiveSeconds(30)
                .setMaxIdleSeconds(30)
                .setEvictionConfig(evictionConfig);
        config.addMapConfig(mapUserConfig);
        return config;
    }

}